/*
 * printf_bsp.c
 *
 *  Created on: Dec 8, 2020
 *      Author: Administrator
 */

#include	"printf_bsp.h"
#include	"stdio.h"
#include	"stdlib.h"

uint16_t	uart4_idle_cnt = 0;
uint16_t	uart6_idle_cnt = 0;
uint16_t	uart4_idle_flag = 0;
uint16_t	uart6_idle_flag = 0;

uint32_t 	uart_isrflags = 0;


uint16_t	uart4_RxCpltCallback_cnt = 0;
uint16_t	uart6_RxCpltCallback_cnt = 0;
uint16_t	uart4_RxCpltCallback_flag = 0;
uint16_t	uart6_RxCpltCallback_flag = 0;



#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

PUTCHAR_PROTOTYPE
{
	HAL_UART_Transmit(&huart4, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
//	HAL_UART_Transmit_IT(&huart4, (uint8_t*)&ch, 1);
//	HAL_UART_Transmit_DMA(&huart4, (uint8_t*)&ch, 1);
    return ch;
}
#endif



uint32_t	uart6_rx_cnt = 0;
uint32_t	uart4_rx_cnt = 0;

uint8_t		uart4_t_buff[UART4_TBUF_LEN] = {'a','b','c','d','e','f'};
uint8_t		uart4_r_buff[UART4_RBUF_LEN] = {0};

uint8_t		uart6_t_buff[UART6_TBUF_LEN] = {'a','b','c','d','e','f'};
uint8_t		uart6_r_buff[UART6_RBUF_LEN] = {0};

void	printf_sysinfo_process(void)
{
	static	uint32_t	printf_stamp = 0;
	static  int32_t		print_cnt=0;
	int i;
	if(HAL_GetTick()-printf_stamp >= PRINTF_SYSINFO_SPAN)
	{
		printf_stamp = HAL_GetTick();

//		printf("Systick(ms):%d\r\n", printf_stamp);
//		print_cnt++;
//		printf ("Print_cnt:%ld\n", print_cnt);

//		log("Systick(ms):%d", printf_stamp);
//		for(i=0;i<100;i++)
//		{
//			uart4_t_buff[i] = (uint8_t) i+rand()%15;
//		}
//		HAL_UART_Transmit_DMA(&huart4, uart4_t_buff, 100);

	}
}

extern	uint16_t	tim7_it_cnt;
extern	uint16_t	tim7_it_flag;

extern	uint32_t	mb_rec_byte_cnt;
uint32_t	mb_rec_byte_cnt_prt = 0;

extern	uint32_t	mb_send_byte_cnt;
uint32_t	mb_send_byte_cnt_prt = 0;


void	debug_print_rx_data(void)
{

	if(uart4_RxCpltCallback_flag > 0)
	{
		uart4_RxCpltCallback_flag = 0;
		log("uart4_RxCpltCallback_cnt: %d, rx_cnt: %d\r\n", \
				uart4_RxCpltCallback_cnt, uart4_rx_cnt);
//		log("%x,%x,%x,%x,%x\r\n", uart4_t_buff[0], uart4_t_buff[1], uart4_t_buff[2]\
//							, uart4_t_buff[3], uart4_t_buff[4]);
	}
	if(uart6_RxCpltCallback_flag > 0)
	{
		uart6_RxCpltCallback_flag = 0;
		log("uart6_RxCpltCallback_cnt: %d, rx_cnt: %d\r\n", \
				uart6_RxCpltCallback_cnt, uart6_rx_cnt);
//		log("%x,%x,%x,%x,%x\r\n", uart6_t_buff[0], uart6_t_buff[1], uart6_t_buff[2]\
//				, uart6_t_buff[3], uart6_t_buff[4]);
	}

	if(tim7_it_flag > 0)
	{
		tim7_it_flag = 0;
		log("tim7_it_cnt: %d\r\n", tim7_it_cnt);
	}

	if(mb_rec_byte_cnt_prt != mb_rec_byte_cnt)
	{
		mb_rec_byte_cnt_prt = mb_rec_byte_cnt;
		log("mb_rec_byte_cnt:%d\r\n", mb_rec_byte_cnt);
	}

	if(mb_send_byte_cnt_prt != mb_send_byte_cnt)
	{
		mb_send_byte_cnt_prt = mb_send_byte_cnt;
		log("mb_send_byte_cnt:%d\r\n", mb_send_byte_cnt);
	}
}


void prvvUARTTxReadyISR( void );
void prvvUARTRxISR( void );

void HAL_UART_TxcpltCallback(UART_HandleTypeDef *huart)
{
	prvvUARTTxReadyISR();
}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == UART4)
	{

//		int16_t rlen = 0;
//		rlen = huart->RxXferCount;
////		log("RxCpltCallback: rlen=%d\r\n", rlen);		// 中断函数调用printf会出错
		memcpy(uart4_t_buff, uart4_r_buff, 5);
//        HAL_UART_Transmit_DMA(huart, uart4_t_buff, 10);
//        HAL_UART_Transmit(huart, uart4_t_buff, 10, 10);
//        HAL_UART_Receive_DMA(huart, uart4_r_buff, 10);

		uart4_RxCpltCallback_flag = 1;
		uart4_RxCpltCallback_cnt ++;

		uart4_rx_cnt += UART4_RX_IT_LEN_CALLBACK;

		HAL_UART_Receive_IT(&huart4, uart4_r_buff, UART4_RX_IT_LEN_CALLBACK);

	}
	else if(huart->Instance == USART6)
	{
//		int16_t rlen = 0;
//		rlen = huart->RxXferCount;
//		memcpy(uart6_t_buff, uart6_r_buff, 5);

		// Todo
		prvvUARTRxISR();

//		uart6_RxCpltCallback_flag = 1;
//		uart6_RxCpltCallback_cnt ++;
//		uart6_rx_cnt+= UART6_RX_IT_LEN_CALLBACK;

		HAL_UART_Receive_IT(&huart6, uart6_r_buff, UART6_RX_IT_LEN_CALLBACK);

	}
	else
	{
		//
		while(1)
		{
			;
		}
	}

}





